package in.muton.edushare.components.autocomplete.api;

import in.muton.edushare.wicket.models.CayenneList;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.model.IModel;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class MainInviter implements Serializable {
	private static final long serialVersionUID = -3219270995815681961L;
	public static final Log log = LogFactory.getLog(MainInviter.class);

	public MainInviter(ORMInterface orm) {
		courseHelper = new CourseAudienceHelper(orm);
		deptHelper = new DeptAudienceHelper(orm);
		univHelper = new UnivAudienceHelper(orm);
		userHelper = new UserHelper(orm);
		groupHelper = new GroupAudienceHelper(orm);
		batchHelper = new BatchAudienceHelper(orm);
	}

	public void setString(String s) {
		origString = s;
		copyString = s;
		startingStrings = NewAudienceHelper.breakString(s);
	}

	public String getOptions(String s) {
		List<String> retList = new ArrayList<String>();
		retList.addAll(courseHelper.getOptions(s));
		retList.addAll(deptHelper.getOptions(s));
		retList.addAll(univHelper.getOptions(s));
		retList.addAll(userHelper.getOptions(s));
		retList.addAll(batchHelper.getOptions(s));
		String ret = "";
		for (String r : retList) {
			ret += r;
		}
		return ret;
	}

	public List<Pair<IModel<?>, CayenneList<Users>>> getCourseAudiences() {
		List<Pair<IModel<?>, CayenneList<Users>>> map = new ArrayList<Pair<IModel<?>, CayenneList<Users>>>();
		Iterator<String> it = startingStrings.iterator();
		while (it.hasNext()) {
			String s = it.next();
			IModel<CourseInstances> ci = courseHelper.getCourse(s);
			if (ci == null)
				continue;
			map.add(new Pair<IModel<?>, CayenneList<Users>>(ci, courseHelper
					.getAllUsers(s), courseHelper.getRoleId(s)));
			it.remove();
			listOfAudiences.add(s);
		}
		log.debug("Course audience size = "+ map.size());
		return map;
	}

	public List<Pair<IModel<?>, CayenneList<Users>>> getUnivAudiences() {
		List<Pair<IModel<?>, CayenneList<Users>>> map = new ArrayList<Pair<IModel<?>, CayenneList<Users>>>();
		Iterator<String> it = startingStrings.iterator();
		while (it.hasNext()) {
			String s = it.next();
			IModel<Universities> ci = univHelper.getUniv(s);
			if (ci == null)
				continue;
			map.add(new Pair<IModel<?>, CayenneList<Users>>(ci, univHelper
					.getAllUsers(s), univHelper.getRoleId(s)));
			it.remove();
			listOfAudiences.add(s);
		}
		log.debug("Univ audience size = "+ map.size());
		return map;
	}

	public List<Pair<IModel<?>, CayenneList<Users>>> getDeptAudiences() {
		List<Pair<IModel<?>, CayenneList<Users>>> map = new ArrayList<Pair<IModel<?>, CayenneList<Users>>>();
		Iterator<String> it = startingStrings.iterator();
		while (it.hasNext()) {
			String s = it.next();
			IModel<Departments> ci = deptHelper.getDept(s);
			if (ci == null)
				continue;
			map.add(new Pair<IModel<?>, CayenneList<Users>>(ci, deptHelper
					.getAllUsers(s), deptHelper.getRoleId(s)));
			it.remove();
			listOfAudiences.add(s);
		}
		log.debug("Dept audience size = "+ map.size());
		return map;
	}
	
	private List<Pair<IModel<?>, CayenneList<Users>>> getBatchAudiences() {
		List<Pair<IModel<?>, CayenneList<Users>>> map = new ArrayList<Pair<IModel<?>, CayenneList<Users>>>();
		Iterator<String> it = startingStrings.iterator();
		while (it.hasNext()) {
			String s = it.next();
			IModel<Batches> ci = batchHelper.getBatch(s);
			if (ci == null)
				continue;
			map.add(new Pair<IModel<?>, CayenneList<Users>>(ci, batchHelper
					.getAllUsers(s), batchHelper.getRoleId(s)));
			it.remove();
			listOfAudiences.add(s);
		}
		log.debug("Batch audience size = "+ map.size());
		return map;
	}

	public List<Pair<IModel<?>, CayenneList<Users>>> getGroupAudiences() {
		List<Pair<IModel<?>, CayenneList<Users>>> map = new ArrayList<Pair<IModel<?>, CayenneList<Users>>>();
		Iterator<String> it = startingStrings.iterator();
		while (it.hasNext()) {
			String s = it.next();
			IModel<Groups> ci = groupHelper.getGroup(s);
			if (ci == null)
				continue;
			map.add(new Pair<IModel<?>, CayenneList<Users>>(ci, groupHelper
					.getAllUsers(s), groupHelper.getRoleId(s)));
			it.remove();
			listOfAudiences.add(s);
		}
		log.debug("Group audience size = "+ map.size());
		return map;
	}

	public List<Pair<IModel<?>, CayenneList<Users>>> getUserAudiences() {
		List<Pair<IModel<?>, CayenneList<Users>>> map = new ArrayList<Pair<IModel<?>, CayenneList<Users>>>();
		CayenneList<Users> ul = getAllSingleUsers();
		if(ul.getObject().size()>0){
			map.add(new Pair<IModel<?>, CayenneList<Users>>(null,
					ul, Poll.POLL_INDI));
		}
		log.debug("User audience size = "+ map.size());
		return map;
	}

	public CayenneList<Users> getSingleAudienceList() {
		CayenneList<Users> cL = new CayenneList<Users>(null);
		List<Pair<IModel<?>, CayenneList<Users>>> list = getAudienceLists();
		for (Pair<IModel<?>, CayenneList<Users>> p : list) {
			cL.add(p.getV2());
		}
		return cL;
	}

	public String[] getUnidentifiedUsers() {
		String[] strArray = new String[startingStrings.size()];
		for (int i = 0; i < startingStrings.size(); i++) {
			strArray[i] = startingStrings.get(i);
		}
		return strArray;
	}

	public List<Pair<IModel<?>, CayenneList<Users>>> getAudienceLists() {
		List<Pair<IModel<?>, CayenneList<Users>>> retList = new ArrayList<Pair<IModel<?>, CayenneList<Users>>>();
		retList.addAll(getCourseAudiences());
		retList.addAll(getUnivAudiences());
		retList.addAll(getDeptAudiences());
		retList.addAll(getBatchAudiences());
		retList.addAll(getGroupAudiences());
		retList.addAll(getUserAudiences());
		log.debug("Total audience size = "+ retList.size());
		return retList;
	}

	public CayenneList<Users> getAllSingleUsers() {
		CayenneList<Users> ul = new CayenneList<Users>(null);
		Iterator<String> it = startingStrings.iterator();
		while (it.hasNext()) {
			String s = it.next();
			IModel<Users> ci = userHelper.getUser(s);
			if (ci == null)
				continue;
			ul.add(ci);
			it.remove();
			listOfAudiences.add(s);
		}
		return ul;
	}

	List<String> listOfAudiences = new ArrayList<String>();
	List<String> startingStrings = new ArrayList<String>();
	public String copyString = "";
	public String origString = "";

	private CourseAudienceHelper courseHelper;
	private DeptAudienceHelper deptHelper;
	private UnivAudienceHelper univHelper;
	private UserHelper userHelper;
	private BatchAudienceHelper batchHelper;
	private GroupAudienceHelper groupHelper;
}
